# Create your views here.

from django.shortcuts import render_to_response
from guestbook.models import Message
from forms import CreateMessageForm
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.paginator import Paginator
def list_messages(request):
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    p = Paginator(Message.objects.order_by('-id'),10)
    messages = p.page(page)

    return render_to_response('list_messages.html',
		{'messages':messages,
                'form':CreateMessageForm()})

def add_message(request):
    message_form = CreateMessageForm(request.POST)
    message = message_form.save()
    try:
        parent_id =int(message_form.cleaned_data['parent_message'])
        message.parent_message = Message.objects.get(id=parent_id)
    except:
        pass
    message.save()
    return HttpResponseRedirect('/guestbook/')

def hello_world(request):
	return HttpResponse('Hello, world!')
